package com.android.bolos;

import java.text.DecimalFormat;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class Detalleconcurso extends Activity {
	private SQLiteDatabase baseDatos;   
	private static final String TAG = "bdbolos";   
	static final String nombreBD = "concursos";   
	public TextView  mediatiro, mediabirle, mediatotal, sumatiro, sumabirle, sumatotal, total2, hora, tipo, lugar, fecha, tiro1, tiro2, tiro3, tiro4, tiro5, tiro6, tiro7, tiro8, birle1, birle2, birle3, birle4, birle5, birle6, birle7, birle8, totacum1, mano1, mano2, mano3, mano4, mano5, mano6, mano7, mano8, totacum2, totacum3, totacum4, totacum5, totacum6, totacum7, totacum8, totalgeneral;

	//guardamos en un String toda la creación de la tabla        
	  private static final String creartablageneral = "create table if not exists "  
		  + " general (_id integer primary key autoincrement, "  
	  	  + " fecha text not null, " 
	  	  + " tiro1 text not null, tiro2 text not null, tiro3 text not null, tiro4 text not null, tiro5 text not null, tiro6 text not null, tiro7 text not null, tiro8 text not null, "
	  	  + " birle1 text not null, birle2 text not null, birle3 text not null, birle4 text not null, birle5 text not null, birle6 text not null, birle7 text not null, birle8 text not null, "
		  + " totalgeneral text not null, lugar text, tipo text, hora text );";
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.detalleconcurso);
	    // TODO Auto-generated method stub
// Asignamos los objetos
	    this.fecha = (TextView) findViewById(R.id.fecha);
	    this.hora = (TextView) findViewById(R.id.hora);
	    this.lugar = (TextView) findViewById(R.id.lugar);
	    this.tipo = (TextView) findViewById(R.id.tipo);
	    this.totalgeneral = (TextView) findViewById(R.id.totalgeneral);
	    this.tiro1 = (TextView) findViewById(R.id.tiro1);
	    this.tiro2 = (TextView) findViewById(R.id.tiro2);
	    this.tiro3 = (TextView) findViewById(R.id.tiro3);
	    this.tiro4 = (TextView) findViewById(R.id.tiro4);
	    this.tiro5 = (TextView) findViewById(R.id.tiro5);
	    this.tiro6 = (TextView) findViewById(R.id.tiro6);
	    this.tiro7 = (TextView) findViewById(R.id.tiro7);
	    this.tiro8 = (TextView) findViewById(R.id.tiro8);
	    this.birle1 = (TextView) findViewById(R.id.birle1);
	    this.birle2 = (TextView) findViewById(R.id.birle2);
	    this.birle3 = (TextView) findViewById(R.id.birle3);
	    this.birle4 = (TextView) findViewById(R.id.birle4);
	    this.birle5 = (TextView) findViewById(R.id.birle5);
	    this.birle6 = (TextView) findViewById(R.id.birle6);
	    this.birle7 = (TextView) findViewById(R.id.birle7);
	    this.birle8 = (TextView) findViewById(R.id.birle8);
	    this.mano1 = (TextView) findViewById(R.id.mano1);
	    this.mano2 = (TextView) findViewById(R.id.mano2);
	    this.mano3 = (TextView) findViewById(R.id.mano3);
	    this.mano4 = (TextView) findViewById(R.id.mano4);
	    this.mano5 = (TextView) findViewById(R.id.mano5);
	    this.mano6 = (TextView) findViewById(R.id.mano6);
	    this.mano7 = (TextView) findViewById(R.id.mano7);
	    this.mano8 = (TextView) findViewById(R.id.mano8);
	    this.totacum1 = (TextView) findViewById(R.id.totacum1);
	    this.totacum2 = (TextView) findViewById(R.id.totacum2);
	    this.totacum3 = (TextView) findViewById(R.id.totacum3);
	    this.totacum4 = (TextView) findViewById(R.id.totacum4);
	    this.totacum5 = (TextView) findViewById(R.id.totacum5);
	    this.totacum6 = (TextView) findViewById(R.id.totacum6);
	    this.totacum7 = (TextView) findViewById(R.id.totacum7);
	    this.totacum8 = (TextView) findViewById(R.id.totacum8);
	    this.sumatiro = (TextView) findViewById(R.id.sumatiro);
	    this.sumabirle = (TextView) findViewById(R.id.sumabirle);
	    this.sumatotal = (TextView) findViewById(R.id.sumatotal);
	    this.total2 = (TextView) findViewById(R.id.total2);
	    this.mediatiro = (TextView) findViewById(R.id.mediatiro);
	    this.mediabirle = (TextView) findViewById(R.id.mediabirle);
	    this.mediatotal = (TextView) findViewById(R.id.mediatotal);
	    // Obtener el identificador

	    	Bundle b = getIntent().getExtras();
	    	 abrirBasedatos();
	    	String identificador = (String) b.get("identificador");
	    	Cursor detalle = baseDatos.rawQuery("SELECT * FROM general WHERE _id = " + identificador, null);
		
			Toast.makeText(this, identificador, Toast.LENGTH_LONG).show();
			detalle.moveToFirst();
			// asignamos los valores
			




			fecha.setText(detalle.getString(1));
			
			hora.setText(detalle.getString(21));
			totalgeneral.setText(detalle.getString(18));
			tipo.setText(detalle.getString(20));
			lugar.setText(detalle.getString(19));
			tiro1.setText(detalle.getString(2));
			tiro2.setText(detalle.getString(3));
			tiro3.setText(detalle.getString(4));
			tiro4.setText(detalle.getString(5));
			tiro5.setText(detalle.getString(6));
			tiro6.setText(detalle.getString(7));
			tiro7.setText(detalle.getString(8));
			tiro8.setText(detalle.getString(9));
			birle1.setText(detalle.getString(10));
			birle2.setText(detalle.getString(11));
			birle3.setText(detalle.getString(12));
			birle4.setText(detalle.getString(13));
			birle5.setText(detalle.getString(14));
			birle6.setText(detalle.getString(15));
			birle7.setText(detalle.getString(16));
			birle8.setText(detalle.getString(17));
			sumatotal.setText(detalle.getString(18));
			total2.setText(detalle.getString(18));
			// sumamos y asignamos los parciales de cada mano
			mano1.setText(Integer.toString(Integer.parseInt(detalle.getString(2)) + Integer.parseInt(detalle.getString(10)))) ;
			mano2.setText(Integer.toString(Integer.parseInt(detalle.getString(3)) + Integer.parseInt(detalle.getString(11)))) ;
			mano3.setText(Integer.toString(Integer.parseInt(detalle.getString(4)) + Integer.parseInt(detalle.getString(12)))) ;
			mano4.setText(Integer.toString(Integer.parseInt(detalle.getString(5)) + Integer.parseInt(detalle.getString(13)))) ;
			mano5.setText(Integer.toString(Integer.parseInt(detalle.getString(6)) + Integer.parseInt(detalle.getString(14)))) ;
			mano6.setText(Integer.toString(Integer.parseInt(detalle.getString(7)) + Integer.parseInt(detalle.getString(15)))) ;
			mano7.setText(Integer.toString(Integer.parseInt(detalle.getString(8)) + Integer.parseInt(detalle.getString(16)))) ;
			mano8.setText(Integer.toString(Integer.parseInt(detalle.getString(9)) + Integer.parseInt(detalle.getString(17)))) ;
			// sumamos y asignamos los parciales acumulados
			totacum1.setText(Integer.toString(Integer.parseInt(detalle.getString(2)) + Integer.parseInt(detalle.getString(10)))) ;
			String Cadena1, Cadena2;
			int Numero1, Numero2;
			Cadena1 = this.totacum1.getText().toString();
			Cadena2 = this.mano2.getText().toString();
			Numero1 = Integer.parseInt(Cadena1);
			Numero2 = Integer.parseInt(Cadena2);
			totacum2.setText(Integer.toString(Numero1 + Numero2)) ;
			Cadena1 = this.totacum2.getText().toString();
			Cadena2 = this.mano3.getText().toString();
			Numero1 = Integer.parseInt(Cadena1);
			Numero2 = Integer.parseInt(Cadena2);
			totacum3.setText(Integer.toString(Numero1 + Numero2)) ;
			Cadena1 = this.totacum3.getText().toString();
			Cadena2 = this.mano4.getText().toString();
			Numero1 = Integer.parseInt(Cadena1);
			Numero2 = Integer.parseInt(Cadena2);
			totacum4.setText(Integer.toString(Numero1 + Numero2)) ;
			Cadena1 = this.totacum4.getText().toString();
			Cadena2 = this.mano5.getText().toString();
			Numero1 = Integer.parseInt(Cadena1);
			Numero2 = Integer.parseInt(Cadena2);
			totacum5.setText(Integer.toString(Numero1 + Numero2)) ;
			Cadena1 = this.totacum5.getText().toString();
			Cadena2 = this.mano6.getText().toString();
			Numero1 = Integer.parseInt(Cadena1);
			Numero2 = Integer.parseInt(Cadena2);
			totacum6.setText(Integer.toString(Numero1 + Numero2)) ;
			Cadena1 = this.totacum6.getText().toString();
			Cadena2 = this.mano7.getText().toString();
			Numero1 = Integer.parseInt(Cadena1);
			Numero2 = Integer.parseInt(Cadena2);
			totacum7.setText(Integer.toString(Numero1 + Numero2)) ;
			Cadena1 = this.totacum7.getText().toString();
			Cadena2 = this.mano8.getText().toString();
			Numero1 = Integer.parseInt(Cadena1);
			Numero2 = Integer.parseInt(Cadena2);
			totacum8.setText(Integer.toString(Numero1 + Numero2)) ;
			// sumas de tiro y de birle
			int sumatiros = 0;
			for (int i=2; i < 10; i++) {
				 sumatiros = sumatiros + Integer.parseInt(detalle.getString(i));
				}
			sumatiro.setText(Integer.toString(sumatiros));
			int sumabirles = 0;
			for (int i=10; i < 18; i++) {
				 sumabirles = sumabirles + Integer.parseInt(detalle.getString(i));
				}
			sumabirle.setText(Integer.toString(sumabirles));
			//calculo de medias
			double tiradas = 8;
			double mediatiros = (sumatiros / tiradas);
			double mediabirles = sumabirles / tiradas;
			double mediatotal2 = (Integer.parseInt(detalle.getString(18))) / tiradas;
			
			DecimalFormat df = new DecimalFormat("0.00"); 
			Log.v("mediatiros", df.format(mediatiros));
	
			mediatiro.setText(df.format(mediatiros));
			mediabirle.setText(String.format("%.2f", mediabirles));
			mediatotal.setText(String.valueOf((mediatotal2)));
			
	} // acaba el oncreate
	private void abrirBasedatos() {
		// TODO Auto-generated method stub
		try 
	      {   
	        baseDatos = openOrCreateDatabase(nombreBD, MODE_WORLD_WRITEABLE, null);   
	        baseDatos.execSQL(creartablageneral);   
	      }    
	      catch (Exception e)
	      {   
	        Log.i(TAG, "Error al abrir o crear la base de datos" + e);   
	      }
	}

	}


